package com.data.modules.logo.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.common.api.vo.CrmResult;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.query.QueryGenerator;
import com.data.modules.logo.entity.SysLogo;
import com.data.modules.logo.service.ISysLogoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: 系统logo相关表
 * @Author: data-boot
 * @Date:   2022-05-25
 * @Version: V1.0
 */
@Api(tags="系统logo相关表")
@RestController
@RequestMapping("/logo/sysLogo")
@Slf4j
public class SysLogoController extends BootController<SysLogo, ISysLogoService> {
	@Autowired
	private ISysLogoService sysLogoService;

	/**
	 * 分页列表查询
	 *
	 * @param sysLogo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "系统logo相关表-分页列表查询")
	@ApiOperation(value="系统logo相关表-分页列表查询", notes="系统logo相关表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SysLogo sysLogo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SysLogo> queryWrapper = QueryGenerator.initQueryWrapper(sysLogo, req.getParameterMap());
		Page<SysLogo> page = new Page<SysLogo>(pageNo, pageSize);
		IPage<SysLogo> pageList = sysLogoService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param sysLogo
	 * @return
	 */
	@AutoLog(value = "系统logo相关表-添加")
	@ApiOperation(value="系统logo相关表-添加", notes="系统logo相关表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SysLogo sysLogo) {

		//确定每个系统唯一
		SysLogo one = sysLogoService.lambdaQuery().eq(SysLogo::getTypes, sysLogo.getTypes())
				.last("limit 1").one();

		if (ObjectUtil.isNotEmpty(one)){
			return Result.error("该系统logo已存在，请勿重复添加！");
		}
		sysLogoService.save(sysLogo);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param sysLogo
	 * @return
	 */
	@AutoLog(value = "系统logo相关表-编辑")
	@ApiOperation(value="系统logo相关表-编辑", notes="系统logo相关表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SysLogo sysLogo) {

		//确定每个系统唯一
		SysLogo one = sysLogoService.lambdaQuery().eq(SysLogo::getTypes, sysLogo.getTypes()).ne(SysLogo::getId,sysLogo.getId())
				.last("limit 1").one();

		if (ObjectUtil.isNotEmpty(one)){
			return Result.error("该系统logo已存在，请勿重复操作！");
		}
		sysLogoService.updateById(sysLogo);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "系统logo相关表-通过id删除")
	@ApiOperation(value="系统logo相关表-通过id删除", notes="系统logo相关表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		sysLogoService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "系统logo相关表-批量删除")
	@ApiOperation(value="系统logo相关表-批量删除", notes="系统logo相关表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysLogoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "系统logo相关表-通过id查询")
	@ApiOperation(value="系统logo相关表-通过id查询", notes="系统logo相关表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SysLogo sysLogo = sysLogoService.getById(id);
		if(sysLogo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(sysLogo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param sysLogo
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysLogo sysLogo) {
        return super.exportXls(request, sysLogo, SysLogo.class, "系统logo相关表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysLogo.class);
    }


	 /**
	  *
	  * @param
	  * @return
	  */
	 @ApiOperation("查询所有系统的logo")
	 @PostMapping("/querySysLogoList")
	 public CrmResult<Object> querySysLogoList(){
	 	log.info("======querySysLogoList===");
		 LambdaQueryWrapper<SysLogo> wrapper = new LambdaQueryWrapper<SysLogo>();
		 wrapper.eq(SysLogo::getIsEnable,'是');
		 List<SysLogo> list = sysLogoService.list(wrapper);
		 return CrmResult.ok(list);
	 }
	 /**
	  *
	  * @param
	  * @return
	  */
	 @ApiOperation("getSysLogoList")
	 @RequestMapping(value = "/getSysLogoList", method = RequestMethod.GET)
	 public CrmResult<Object> getSysLogoList(){
		 log.info("======getSysLogoList===");
		 LambdaQueryWrapper<SysLogo> wrapper = new LambdaQueryWrapper<SysLogo>();
		 wrapper.eq(SysLogo::getIsEnable,'是');
		 List<SysLogo> list = sysLogoService.list(wrapper);
		 return CrmResult.ok(list);
	 }

}
